﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX.UI
{
    class Title : Window
    {
        public Menu MainMenu;
        public Texture2D Logo;

        public Rectangle LogoRect;

        public Title(MainGame game) 
            : base(game)
        {
            MainMenu = new Menu(game);

            int screenWidth = game.Graphics.PreferredBackBufferWidth;
            int screenHeight = game.Graphics.PreferredBackBufferHeight;

            int width = screenWidth * 2 / 5;
            int height = screenHeight * 2 / 5;
            int left = (screenWidth - width) / 2;
            int top = (screenHeight - height) * 2 / 3;
            
            MainMenu.SetBoundingBox(new Rectangle(
                left, top, width, height));

            MainMenu.AddItems(new string[] {
                "Start Game",
                "Load Game",
                "Go to Level",
                "High Scores",
                "Options",
                "Credit",
                "Exit"
            });

            MainMenu.ItemSelected += new Menu.ItemSelectedDelegate(ItemSelected);

            this.AddChild(MainMenu);

            Logo = game.Content.Load<Texture2D>("Logo");
            int logoHeight = (int)width * Logo.Height / Logo.Width;
            LogoRect = new Rectangle(left, (int)(top * 0.4), width, logoHeight);
        }

        public void ItemSelected(Menu menu, int index)
        {
            if (index < 0)
                return;
            String textValue = menu.Items[index].Value;
            if (textValue == "Start Game")
            {   // start game
                /*
                 * build first level
                 */
                Game.RootWindow.CloseAllChildren();

                Introduction intro = new Introduction((MainGame)this.Game);
                intro.Start();
                /*
                MainGame game = (MainGame)this.Game;
                if (game.BuildAndStartLevel(1))
                {
                    this.Close();
                }*/
            }
            else if (textValue == "High Scores")
            {
                HighScores hs = new HighScores(((MainGame)this.Game), this);
                hs.Show();
            }
            else if (textValue == "Exit")
            {
                ((MainGame)this.Game).Quit();
            }
            else if (textValue == "Go to Level")
            {
                LevelSelector ls = new LevelSelector(((MainGame)this.Game), this);
                ls.Show();
            }
            else if (textValue == "Credit")
            {
                MessageDialog credit = new MessageDialog(((MainGame)this.Game), this,
                    "So far this simple game\nis solely created\nby Lincoln Yu.\nSuggestion or help of\nany kind is welcome.");
                credit.Show();
            }
            else
            {
                MessageDialog un = new MessageDialog(((MainGame)this.Game), this);
                un.Show();
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            sBatch.Draw(Logo, LogoRect, Color.White);

            base.Draw(gameTime);
        }

        public override void Show()
        {
            base.Show();
            ((MainGame)Game).SoundResources.PlayTheme();
        }
    }
}
